<?php
class Source_Config
{
	/**
	 * Singleton instance
	 * 
	 * @var Source_Config
	 */
	private static $_instance;
	
	/**
	 * Configuration data
	 * 
	 * @var Zend_Config
	 */
	private $_data;
	
	/**
	 * Singleton instance fetcher
	 * 
	 * @return Source_Config
	 */
	public static function getInstance()
	{
		if(null === self::$_instance){
			self::$_instance = new self();
		}	
		return self::$_instance;
	}
	
	/**
	 * Fetch data
	 * 
	 * @param string|null $path
	 * @return mixed
	 */
	public static function get($path = null)
	{
		return self::getInstance()->getData($path);	
	}
	
	/**
	 * Add data
	 * 
	 * @param Zend_Config $data
	 */
	public static function add(Zend_Config $data)
	{
		self::getInstance()->addData($data);	
	}
	
	/**
	 * Constructor
	 */
	private function __construct()
	{
		$this->setData(new Zend_Config(array(),true));	
	}
	
	/**
	 * Fetch data
	 * 
	 * @param string $path
	 * @return mixed
	 */
	public function getData($path = null)
	{
		$data = $this->_data;
		if(null !== $path){
			$parts = explode('/',$path);
			foreach($parts as $part){
				$data = $data->get($part);
			}
		}
		return $data;			
	}
	
	/**
	 * Sets data
	 * 
	 * @param Zend_Config $data
	 * @return mixed
	 */
	public function setData(Zend_Config $data)
	{
		$this->_data = $data;
		return $this;
	}
	
	/**
	 * Merges data
	 * 
	 * @param Zend_Config $data
	 */
	public function addData(Zend_Config $data)
	{
		$this->getData()->merge($data);
		return $this;
	}
}